﻿Imports DTO
Imports BUS
Public Class ucNhomHang

    Dim nh As New NhomHangDTO

    Private Sub barbtnThemNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnThemNH.ItemClick
        Dim frm As New frmThemNhomHang
        AddHandler frm.LamMoiNVKhiThem, AddressOf LayDSNhomHang
        frm.ShowDialog()
    End Sub

    Sub LayDSNhomHang()
        GridControl1.DataSource = NhomHangBUS.LayDSNhomHang()
    End Sub


    Private Sub barbtnSuaChuaNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnSuaChuaNH.ItemClick
        Dim frm As New frmThemNhomHang
        frm.Text = "Sửa chữa nhóm hàng"
        frm.kt = 1
        frm.tenNH = nh.TenNhomHang
        frm.maNH = nh.MaNhomHang
        frm.ghichu = nh.GhiChu
        frm.maTT = nh.MaTrangThai
        frm.ShowDialog()
        
    End Sub

    Private Sub barbtnXoaNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnXoaNH.ItemClick
        Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 23)
        If pq.Xoa = True Then
            Try
                If NhomHangBUS.XoaNhomHang(nh) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thành công nhóm hàng", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    LayDSNhomHang()
                End If
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show("Nhóm hàng này đang sử dụng không thể xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                NhomHangBUS.DongKetNoi.Close()
            End Try
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        
    End Sub

    Private Sub barbtnNapLaiNH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnNapLaiNH.ItemClick
        LayDSNhomHang()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        If e.RowHandle >= 0 Then
            barbtnSuaChuaNH.Enabled = True
            barbtnXoaNH.Enabled = True
            nh.MaNhomHang = GridView1.GetRowCellValue(e.RowHandle, GCMaNH).ToString()
            nh.TenNhomHang = GridView1.GetRowCellValue(e.RowHandle, GCTenNH).ToString()
            nh.GhiChu = GridView1.GetRowCellValue(e.RowHandle, GCGhiChuNH).ToString()
            nh.MaTrangThai = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCQuanLyNH))
        End If
    End Sub

    Private Sub ucNhomHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barbtnSuaChuaNH.Enabled = False
        barbtnXoaNH.Enabled = False
        barbtnXuatNH.Enabled = False
        LayDSNhomHang()
    End Sub

    Private Sub GridView1_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridView1.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub

    Private Sub GridView1_RowStyle(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles GridView1.RowStyle
        If (e.RowHandle >= 0) Then
            Dim category As Integer = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCGhiChuNH))
            If category = 0 Then
                e.Appearance.ForeColor = Color.Red
                e.Appearance.FontStyleDelta = FontStyle.Italic
            End If
        End If
    End Sub
End Class
